package dao.impl;

import dao.BaseDao;
import dao.BlogDao;
import entity.Blog;
import util.Page;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class BlogDaoImpl extends BaseDao implements BlogDao {

    @Override
    public int getBlogCount(int uid) {
        int i=0;
        ResultSet rs=null;
        String sql=" SELECT count(*) FROM blog where uid=? ";
        try {
            rs=executeQuery(sql,uid);
            while (rs.next()){
                i=rs.getInt("count(*)");
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(conn,null,rs);
        }
        return i;
    }

    @Override
    public List<Blog> getBlogList(int uid, String content) {
        List<Blog> blogList=new ArrayList<Blog>();
        ResultSet rs=null;
        String sql=" SELECT * FROM `blog` where uid=? and content like ? ";
        try {
            rs=executeQuery(sql,uid,"%"+content+"%");
            while (rs.next()){
                Blog blog=new Blog();
                blog.setId(rs.getInt("id"));
                blog.setUid(rs.getInt("uid"));
                blog.setContent(rs.getString("content"));
                blog.setImages(rs.getString("images"));
                blog.setTime(rs.getString("time"));
                blog.setState(rs.getInt("state"));
                blog.setFromBid(rs.getInt("fromBid"));
                blogList.add(blog);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(conn,null,rs);
        }
        return blogList;
    }

    //在首页显示数据的sql实现
    @Override
    public List<Blog> getAllBlogList(int uid1, String content1, int uid2, String content2, Page page) {
        List<Blog> blogList=new ArrayList<Blog>();
        ResultSet rs=null;
        String sql="SELECT `user`.nickname as u_nickname, `user`.image as u_image ,blog.id, blog.uid, blog.content, blog.images, blog.time, blog.state, blog.fromBid FROM blog,`user` WHERE blog.uid=`user`.id and blog.uid = ? AND blog.content LIKE ?  UNION SELECT `user`.nickname as u_nickname, `user`.image as u_image ,blog.id, blog.uid, blog.content, blog.images, blog.time, blog.state, blog.fromBid FROM blog,follow,`user` where blog.uid = follow.followedUid and follow.uid = ? and `user`.id=blog.uid AND blog.content LIKE ? order by time desc limit ?,?  ";
        try {
            rs=executeQuery(sql,uid1,"%"+content1+"%",uid2,"%"+content2+"%",page.getFirstRecordIndex(),page.getPageSize());
            while (rs.next()){
                Blog blog=new Blog();
                blog.getUser().setNickname(rs.getString("u_nickname"));
                blog.getUser().setImage(rs.getString("u_image"));
                blog.setId(rs.getInt("id"));
                blog.setUid(rs.getInt("uid"));
                blog.setContent(rs.getString("content"));
                blog.setImages(rs.getString("images"));
                blog.setTime(rs.getString("time"));
                blog.setState(rs.getInt("state"));
                blog.setFromBid(rs.getInt("fromBid"));

                blogList.add(blog);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(conn,null,rs);
        }
        return blogList;
    }

    @Override
    public int allCount(int uid1, String content1,int uid2, String content2) {
        int i=0;
        ResultSet rs=null;
        String sql="SELECT COUNT(*) FROM (SELECT `user`.nickname as u_nickname, `user`.image as u_image, blog.id, blog.uid, blog.content, blog.images, blog.time, blog.state, blog.fromBid FROM blog, `user`  WHERE blog.uid = `user`.id AND blog.uid = ? AND blog.content LIKE ? UNION ALL SELECT `user`.nickname as u_nickname, `user`.image as u_image, blog.id, blog.uid, blog.content, blog.images, blog.time, blog.state, blog.fromBid  FROM blog, follow, `user`  WHERE blog.uid = follow.followedUid AND follow.uid = ? AND `user`.id = blog.uid AND blog.content LIKE ?) AS subquery";
        try{
            rs=executeQuery(sql,uid1,"%"+content1+"%",uid2,"%"+content2+"%");
            while (rs.next()){
                i=rs.getInt("count(*)");
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(conn,null,rs);
        }
        return i;
    }

    @Override
    public Blog getBlogById(int bid) {
        Blog blog=null;
        ResultSet rs=null;
        String sql="select blog.id as bid,`user`.image as u_image,`user`.nickname as u_name,content,blog.images as b_image,time from `user`,blog where `user`.id=blog.uid and blog.id=?";
        try{
            rs=executeQuery(sql,bid);
            while (rs.next()){
                blog=new Blog();
                blog.setId(rs.getInt("bid"));
                blog.getUser().setImage(rs.getString("u_image"));
                blog.getUser().setNickname(rs.getString("u_name"));
                blog.setContent(rs.getString("content"));
                blog.setImages(rs.getString("b_image"));
                blog.setTime(rs.getString("time"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(conn,null,rs);
        }
        return blog;
    }
}
